<?php
namespace app\farm\model;

use app\BaseModel;
use app\shop\model\StoreGoods;
use think\facade\Db;

class Breed extends BaseModel
{
    //定义表名
    protected $name = 'lbfarm_breed';




    /**
     * @param $value
     * @param $data
     * @功能说明:
     * @author chenniang
     * @DataTime: 2022-07-04 17:23
     */
    public function getFarmerIdAttr($value,$data){

        if(!empty($data['id'])){

            $dis = [

                'b.status' => 2,

                'a.goods_id' => $data['id'],

                'a.type'     => 5
            ];

            $store_goods_model = new StoreGoods();

            $list = $store_goods_model->alias('a')
                ->join('lbfarm_farmer b','a.store_id = b.id')
                ->where($dis)
                ->field('a.*,b.title')
                ->group('a.id')
                ->order('a.id dsec')
                ->column('b.id');

            return array_values($list);
        }


    }


    /**
     * @param $dis
     * @param int $page
     * @功能说明:养殖列表
     * @author chenniang
     * @DataTime: 2022-07-12 17:57
     */
    public function breedList($dis,$page=10){

        $data = $this->alias('a')
                ->join('lbfarm_v2_goods_store b','a.id = b.goods_id','left')
                ->where($dis)
                ->field('a.*')
                ->group('a.id')
                ->order('a.top desc,a.id desc')
                ->paginate($page)
                ->toArray();

        return $data;


    }



    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:04
     * @功能说明:添加
     */
    public function dataAdd($data){

        $data['create_time'] = time();

        if(!empty($data['farmer_id'])){

            $farmer = $data['farmer_id'];

            unset($data['farmer_id']);
        }

        $res = $this->insert($data);

        if(isset($farmer)){

            $res = $this->getLastInsID();

            $this->updateSome($res,$farmer,$data['uniacid']);
        }

        return $res;

    }



    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:05
     * @功能说明:编辑
     */
    public function dataUpdate($dis,$data){

        if(!empty($data['farmer_id'])){

            $farmer = $data['farmer_id'];

            unset($data['farmer_id']);
        }

        $res = $this->where($dis)->update($data);

        if(isset($farmer)){

            $this->updateSome($dis['id'],$farmer,$data['uniacid']);
        }

        return $res;

    }


    /**
     * @param $id
     * @param $data
     * @param $uniacid
     * @功能说明:
     * @author chenniang
     * @DataTime: 2022-07-12 17:17
     */
    public function updateSome($id,$data,$uniacid){

        $store_goods_model = new StoreGoods();

        $res = $store_goods_model->addData($id,5,$uniacid,$data);

        return $res;

    }

    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:06
     * @功能说明:列表
     */
    public function dataList($dis,$page=10){

        $data = $this->where($dis)->order('status desc,id desc')->paginate($page)->toArray();

        return $data;

    }


    /**
     * @author chenniang
     * @DataTime: 2022-08-09 16:11
     * @功能说明:列表
     */
    public function indexDataList($dis,$page=10){


        $data = $this->alias('a')
                ->join('lbfarm_v2_goods_store b','a.id = b.goods_id')
                ->where($dis)
                ->field('a.*')
                ->group('a.id')
                ->order('id desc')
                ->paginate($page)
                ->toArray();

        return $data;

    }





    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:43
     * @功能说明:
     */
    public function dataInfo($dis){

        $data = $this->where($dis)->find();

        return !empty($data)?$data->toArray():[];

    }


    /**
     * @author chenniang
     * @DataTime: 2021-03-19 16:08
     * @功能说明:开启默认
     */
    public function updateOne($id){

        $user_id = $this->where(['id'=>$id])->value('user_id');

        $res = $this->where(['user_id'=>$user_id])->where('id','<>',$id)->update(['status'=>0]);

        return $res;
    }


    /**
     * @author chenniang
     * @DataTime: 2021-12-29 17:05
     * @功能说明:获取养殖购物车信息
     */
    public function getCarList($user_id,$farmer_id){

        $dis = [

            'b.user_id'  => $user_id,

            'b.farmer_id'=> $farmer_id,

            'a.status'   => 1,

            'b.type'     => 1,

            'c.type'     => 5,
        ];

        $data = $this->alias('a')
                ->join('lbfarm_car b','a.id = b.goods_id')
                ->join('lbfarm_v2_goods_store c','a.id = c.goods_id')
                ->where($dis)
                ->field('a.*,b.goods_num,b.goods_id,ROUND(a.price*b.goods_num,2) as all_price')
                ->group('b.id')
                ->order('b.id desc')
                ->select()
                ->toArray();

        $arr['car_list'] = $data;

        $arr['price']    = array_sum(array_column($data,'all_price'));

        $arr['goods_num']= array_sum(array_column($data,'goods_num'));

        return $arr;

    }





}